<template>
    <fieldset>
        <legend>登录页</legend>
        用户名：
        <input type="text" v-model="username">
        <br>
        密码
        <input type="password" v-model="password" @keydown.enter="login">
        <br>
        <button @click="login">点击登录</button>
    </fieldset>
</template>
<script setup>
import * as api from "../../api/users"
import {ref} from "vue"
import {useRouter} from "vue-router"
// 1.获取输入框的用户名和密码
let username = ref("")
let password = ref("")
// 2.发起请求
let router = useRouter()
let login = async()=>{
    let res = await api.loginApi(username.value,password.value)
    console.log(res);
    // 判断是否登录成功
    if(res.data.state){
        // 将令牌信息存到本地
        sessionStorage.setItem('user-token',res.data.token)
        // 登录成功，跳转页面
        router.push("/")
    }else{
        alert(res.data.msg)
    }
}


</script>